package com.zhangqi.zhxa;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.zhangqi.zhxa.utils.PrefUtils;


public class SplashActivity extends AppCompatActivity {

    private ImageView rlRoot;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        rlRoot = (ImageView) findViewById(R.id.iv_sp);
        //1,旋转，2.缩放，3.渐变
        //旋转
        RotateAnimation animRotate = new RotateAnimation(0,360,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //在动画开始和结束的时候慢，中间加速
        DecelerateInterpolator dlInterplator = new
                DecelerateInterpolator (1f);

        animRotate.setInterpolator(dlInterplator);

        animRotate.setDuration(3500);
        animRotate.setFillAfter(true);

        //缩放
        ScaleAnimation animScale = new ScaleAnimation(0,1,0,1,
                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animScale.setDuration(1000);
        animScale.setFillAfter(true);

        //渐变
        AlphaAnimation animAlpha = new AlphaAnimation(0,1);
        animAlpha.setFillAfter(true);

        //动画集合
        AnimationSet animSet = new AnimationSet(true);
        animSet.addAnimation(animRotate);
        animSet.addAnimation(animScale);
        animSet.addAnimation(animAlpha);

        //启动
        rlRoot.startAnimation(animSet);

        animSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //判断是否需要调到新手引导
                boolean isGuideShow = PrefUtils.getBoolean("is_guide_show",
                        false, getApplicationContext());

                if(isGuideShow){
                    //动画结束后跳主页面
                    Intent intent = new Intent(SplashActivity.this,MainActivity.class);
                    startActivity(intent);
                }else {
                    //跳到新手引导页面
                    Intent intent = new Intent(SplashActivity.this,GuideActivity.class);
                    startActivity(intent);
                }
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
